package cn.two.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.github.pagehelper.Page;

import cn.two.model.OaApplymoney;
import cn.two.model.OaBorrow;
import cn.two.model.OaDept;
import cn.two.model.OaSubmit;
import cn.two.model.OaUser;
import cn.two.model.Result;
import cn.two.service.OaApplymoneyService;


@RestController
@RequestMapping("/applyMoney")
public class OaApplymoneyController {
	@Resource
	private OaApplymoneyService oaApplymoneyService;
	
	/**
	 * 主视图
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(HttpSession session) {
		
		return new ModelAndView("index");
	}
	
	//查询借款用途表
	@RequestMapping("/searchBorrow")
	public ModelAndView selectBorrow(Model model){
		List<OaBorrow> list = oaApplymoneyService.selectBorrowTable();
		model.addAttribute("borrow", list);
		return new ModelAndView("borrow_money");
	}
	/*// 根据用户id查询借款申请单
	@RequestMapping("/selectApplyByUserId")
	public String selectByPrimaryKey(Model model,HttpSession session) {
		//模拟登录存用户信息到session
		OaUser oaUser = new OaUser();
		oaUser.setUserId(5);
		session.setAttribute("user", oaUser);
		OaUser oaUser2 = (OaUser) session.getAttribute("user");
		int userId = oaUser2.getUserId();
		List<OaApplymoney> list = oaApplymoneyService.selectByPrimaryKey(userId);
		System.out.println(list.size());
		
		model.addAttribute("showApply", list);
		return "show_borrow_money";
	}*/
	//分页，用户查询自己的所有借款申请
	@RequestMapping("/getPage")
	public Map queryUsersByPage(int pageNum, int pageSize,OaApplymoney oaApplymoney,HttpSession session,HttpServletRequest request ){
		String minDate = request.getParameter("startDate");
		String maxDate = request.getParameter("endDate");
		String submit_state = request.getParameter("subState");
		
		OaUser oaUser = (OaUser) session.getAttribute("oauser");
		int userId = oaUser.getUserId();
		oaUser.setUserId(userId);
		oaApplymoney.setOaUser(oaUser);
		Map map = new HashMap();
		map.put("pageSize", pageSize);
		map.put("pageNum", pageNum);
		map.put("oaApplymoney", oaApplymoney);
		map.put("minDate", minDate);
		map.put("maxDate", maxDate);
		map.put("subState", submit_state);
		Page page = oaApplymoneyService.queryLikeUsers(map);
		map.put("page", page);
		map.put("totalPage", page.getPages());
		//在前台页面显示总条数
		map.put("num", page.getTotal());
		return map;
	}
	//分页，查询用户所有的借款申请，只做跳转页面的操作
	@RequestMapping("/showApply")
	public ModelAndView index(){
		return new ModelAndView("show_borrow_money");
	}
	/*//查询用户的个人借款申请
	@RequestMapping("/selectApplyByUserId")
	public ModelAndView selectByPrimaryKey(HttpSession session,Model model){
		//模拟登录存用户信息到session
		OaUser oaUser = new OaUser();
		oaUser.setUserId(5);
		session.setAttribute("user", oaUser);
		OaUser oaUser2 = (OaUser) session.getAttribute("user");
		int userId = oaUser2.getUserId();
		List<OaApplymoney> list = oaApplymoneyService.selectByPrimaryKey(userId);
		model.addAttribute("showApply", list);
		 Map<String,Object> rsMap = new HashMap<String,Object>();
		 rsMap.put("data", list);
		return new  ModelAndView("show_borrow_money",rsMap);
	}*/
	
	
	// 分页， 经理查询所有的借款申请
	@RequestMapping("/getPages")
	public Map queryUsersByPages(int pageNum, int pageSize,OaApplymoney oaApplymoney,HttpSession session,HttpServletRequest request){
		String minDate = request.getParameter("startDate");
		String maxDate = request.getParameter("endDate");
		String submit_state = request.getParameter("subState");
		
		//存入用户的id，，有登录之后这里可以直接替换session
		Map map = new HashMap();
		map.put("pageSize", pageSize);
		map.put("pageNum", pageNum);
		map.put("minDate", minDate);
		map.put("maxDate", maxDate);
		map.put("subState", submit_state);
		map.put("oaApplymoney", oaApplymoney);
		Page page = oaApplymoneyService.queryLikeAllUsers(map);
		map.put("page", page);
		map.put("totalPage", page.getPages());
		map.put("totalPage", page.getPages());
		//在前台页面显示总条数
		map.put("num1", page.getTotal());
		return map;	  
	}
	
	//分页，经理查询所有的借款申请，只做跳转页面的操作
	@RequestMapping("/showApplyAll")
	public ModelAndView indexAll(){
		return new ModelAndView("show_all_borrow_money");
	}
	//多条件查询借款申请
	@RequestMapping("/selectApplyMoneyMore")
	public String selecApplytMore(OaApplymoney oaApplymoney,Model model){
		List<OaApplymoney> list = oaApplymoneyService.selectMore(oaApplymoney);
		model.addAttribute("list", list);
		return "selectApplyMoneyMore";
	}

	
	
	//新增借款申请1
	@RequestMapping("/addApplyMoney")
	public ModelAndView addApplyMoney(HttpSession session,OaApplymoney oaApplymoney,int borrowId){
		OaUser oaUser = (OaUser) session.getAttribute("oauser");
		OaDept oaDept = new OaDept();
		 int deptId = oaUser.getOaDept().getDeptId();
		 oaDept.setDeptId(deptId); 
		OaBorrow oaBorrow = new OaBorrow();
		oaBorrow.setBorrowId(borrowId);
		oaApplymoney.setOaDept(oaDept);
		oaApplymoney.setOaBorrow(oaBorrow);
		oaApplymoney.setOaUser(oaUser);
		int count = oaApplymoneyService.addBorrowMoneyTable(oaApplymoney);
		
		if (count>0) {
			return new ModelAndView("sucess");
		}else{
			return new ModelAndView("sucess");
		}
	}
	//新增借款申请2--返回申请页面，，没有具体的操作
	@RequestMapping("/addBorrowMoney")
	public ModelAndView  addBorrowMoney(Model model){
		List<OaBorrow> list = oaApplymoneyService.selectBorrowTable();
		model.addAttribute("borrow", list);
		return new ModelAndView("addApply");
	}
	//新增借款申请2--进行添加数据的操作
	@RequestMapping("/addApply")
	public Result addApply(OaApplymoney oaApplymoney,HttpSession session,int borrowId){
		OaUser oaUser = (OaUser) session.getAttribute("oauser");
		OaDept oaDept = new OaDept();
		 int deptId = oaUser.getOaDept().getDeptId();
		 oaDept.setDeptId(deptId); 
		OaBorrow oaBorrow = new OaBorrow();
		oaBorrow.setBorrowId(borrowId);
		oaApplymoney.setOaDept(oaDept);
		oaApplymoney.setOaBorrow(oaBorrow);
		oaApplymoney.setOaUser(oaUser);
		oaApplymoneyService.addBorrowMoneyTable(oaApplymoney);
		return new Result();
	}
	//审核借款申请--返回审核借款申请页面，，抓取员工申请数据
		@RequestMapping("/checkBorrowMoney")
		public ModelAndView  checkApplyMoney(HttpSession session,OaApplymoney oaApplymoney){
			/*System.out.println(oaApplymoney.getOaBorrow().getBorrowType());
			OaBorrow oaBorrow = oaApplymoneyService.selectBorrowTableById(oaApplymoney);
			
			session.setAttribute("borrow", oaBorrow);
			
			OaBorrow oaBorrow2  = (OaBorrow) session.getAttribute("borrow");
			System.out.println(oaBorrow2.getBorrowType());*/
			
			OaApplymoney rsOaApplymoney = oaApplymoneyService.selectById(oaApplymoney);
			Map<String , Object> rsMap = new HashMap<String,Object>();
			rsMap.put("data", rsOaApplymoney);
			return new ModelAndView("checkApply",rsMap);
		}
		//查看借款申请--返回视图页面，，抓取员工申请数据
				@RequestMapping("/lookBorrowMoney")
				public ModelAndView  lookApplyMoney(HttpSession session,OaApplymoney oaApplymoney){
					/*System.out.println(oaApplymoney.getOaBorrow().getBorrowType());
					OaBorrow oaBorrow = oaApplymoneyService.selectBorrowTableById(oaApplymoney);
					
					session.setAttribute("borrow", oaBorrow);
					
					OaBorrow oaBorrow2  = (OaBorrow) session.getAttribute("borrow");
					System.out.println(oaBorrow2.getBorrowType());*/
					
					OaApplymoney rsOaApplymoney = oaApplymoneyService.selectById(oaApplymoney);

					Map<String , Object> rsMap = new HashMap<String,Object>();
					rsMap.put("data", rsOaApplymoney);
					return new ModelAndView("lookApply",rsMap);
				}
		
		//审核借款申请，更新数据库的操作
		@RequestMapping("/checkApply")
		public Result editOaApplyMoneys(OaApplymoney oaApplymoney,OaSubmit oaSubmit){
			oaApplymoney.setOaSubmit(oaSubmit);			
			oaApplymoneyService.updateBorrowMoneyTables(oaApplymoney);
			return new Result();
			
		}
	//修改借款申请---抓取数据，显示在页面，没有设置修改的操作
	@RequestMapping("/updateBorrowMoney")
	public ModelAndView  updateBorrowMoney(Model model,OaApplymoney oaApplymoney){
		List<OaBorrow> list = oaApplymoneyService.selectBorrowTable();
		model.addAttribute("borrow", list);
		OaApplymoney rsOaApplymoney = oaApplymoneyService.selectById(oaApplymoney);
		Map<String , Object> rsMap = new HashMap<String,Object>();
		rsMap.put("data", rsOaApplymoney);
		return new ModelAndView("updateApply",rsMap);
	}
	//修改借款申请---更新数据的操作
	@RequestMapping("/updateApply")
	public Result editOaApplyMoney(HttpServletRequest request,HttpSession session,OaApplymoney oaApplymoney,int borrowId){
		OaUser oaUser = (OaUser) session.getAttribute("oauser");
		OaDept oaDept = new OaDept();
		 int deptId = oaUser.getOaDept().getDeptId();
		String appl = request.getParameter("applySubmit");
		 oaDept.setDeptId(deptId); 
		OaBorrow oaBorrow = new OaBorrow();
		oaBorrow.setBorrowId(borrowId);
		oaApplymoney.setOaDept(oaDept);
		oaApplymoney.setOaBorrow(oaBorrow);
		oaApplymoney.setOaUser(oaUser);
		oaApplymoney.setApplySubmit(appl);
		
		oaApplymoneyService.updateBorrowMoneyTable(oaApplymoney);
		return new Result();
		
	}
	
	//删除借款申请
	@RequestMapping("/delBorrowMoney")
	public Result delBorrowMoney(OaApplymoney oaApplymoney){
		oaApplymoneyService.delBorrowMoneyTable(oaApplymoney);
		return new Result();
	}
	
	public OaApplymoneyService getOaApplymoneyService() {
		return oaApplymoneyService;
	}

	public void setOaApplymoneyService(OaApplymoneyService oaApplymoneyService) {
		this.oaApplymoneyService = oaApplymoneyService;
	}

}
